package com.zl_huang.example.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zl_huang.example.model.QueryCondition;
import com.zl_huang.example.model.Subject;
import com.zl_huang.example.model.User;
import com.zl_huang.example.model.vo.ScoreVO;
import com.zl_huang.example.service.IScoreService;
import com.zl_huang.example.service.ISubjectService;
import com.zl_huang.example.service.IUserService;

@Controller
public class ScoreController {
	@Autowired
	private IScoreService scoreService;
	@Autowired
	private IUserService  userService;
	@Autowired
	private ISubjectService subjectServie;
	
	@RequestMapping(value = "/scoreList" , method = RequestMethod.GET)
	public String toScoreList(Model model){
		model.addAttribute("queryCondition", new QueryCondition());
		List<ScoreVO> scoreVOList = null;
		List<String> userIdList = new ArrayList<String>();
		List<String> userNameList = new ArrayList<String>();
		List<String> nameList = new ArrayList<String>();
		try {
		   List<User> userList = userService.findAllUsers();
		   if(!userList.isEmpty()){
			   for(User user : userList){
				   userIdList.add(user.getUser_id());
				   userNameList.add(user.getUser_name());
			   }
			   
		   }
		   List<Subject> subjectList = subjectServie.findAllSubject();
		   if(!subjectList.isEmpty()){
			   for(Subject subject : subjectList){
				   nameList.add(subject.getName());
			   }
		   }
			scoreVOList = scoreService.getAllScore();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("nameList", nameList);
		model.addAttribute("userIdList", userIdList);
		model.addAttribute("userNameList", userNameList);
		model.addAttribute("scoreVOList", scoreVOList);
		return "score/ScoreList";
	}
	
	@RequestMapping(value = "/selectScore" , method = RequestMethod.POST)
	public String selectScore(RedirectAttributes redirectAttributes , @Validated QueryCondition queryCondition,Model model){
		model.addAttribute("queryCondition", new QueryCondition());
		List<ScoreVO> scoreVOList = null;
		List<String> userIdList = new ArrayList<String>();
		List<String> userNameList = new ArrayList<String>();
		List<String> nameList = new ArrayList<String>();
		try {
		   List<User> userList = userService.findAllUsers();
		   if(!userList.isEmpty()){
			   for(User user : userList){
				   userIdList.add(user.getUser_id());
				   userNameList.add(user.getUser_name());
			   }
			   
		   }
		   List<Subject> subjectList = subjectServie.findAllSubject();
		   if(!subjectList.isEmpty()){
			   for(Subject subject : subjectList){
				   nameList.add(subject.getName());
			   }
		   }
			scoreVOList = scoreService.getSelectedScore(queryCondition);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("nameList", nameList);
		model.addAttribute("userIdList", userIdList);
		model.addAttribute("userNameList", userNameList);
		model.addAttribute("scoreVOList", scoreVOList);
		return "score/ScoreList";
	}


}
